package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.modles.ShopDetailsData;

import java.util.List;

/**
 * 单品详情页面 的商城价格 的适配器
 * Created by lanouhn on 16/9/11.
 */
public class ShopPriceAdapter extends RecyclerView.Adapter<ShopPriceAdapter.ShopPriceViewHolder> {


    private Context context;
    private List<ShopDetailsData.DataBean.PurchaseWayBean> purchaseWayBeen;

    public ShopPriceAdapter(Context context, List<ShopDetailsData.DataBean.PurchaseWayBean> purchaseWayBeen) {
        this.context = context;
        this.purchaseWayBeen = purchaseWayBeen;
    }

    @Override
    public ShopPriceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shopdetails_price, null);
        ShopPriceViewHolder holder = new ShopPriceViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ShopPriceViewHolder holder, final int position) {

        holder.taobaoPrice.setText(purchaseWayBeen.get(position).getPrice());
        Picasso.with(context).load(purchaseWayBeen.get(position).getPic()).into(holder.taobaoImage);
        holder.taobaoCankao.setText(purchaseWayBeen.get(position).getName() + "：￥ ");

        //跳到点击的商城
        holder.reOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (purchaseWayBeen.size() > 0) {//purchaseWayBeen是否为空
                    //获取商城网址
                    String urlAlter = purchaseWayBeen.get(position).getUrl();

                    //getPlatform()==1,跳到淘宝
                    if (purchaseWayBeen.get(position).getPlatform() == 1) {//是否为淘宝
                        Uri uriTb = Uri.parse(urlAlter);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uriTb);
                        context.startActivity(intent);
                    }

                    //getPlatform()==2,跳到天猫
                    if (purchaseWayBeen.get(position).getPlatform() == 2) {//是否为天猫

                        Uri uriTm = Uri.parse(urlAlter);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uriTm);
                        context.startActivity(intent);
                    }
                    //getPlatform()==3,跳到京东
                    if (purchaseWayBeen.get(position).getPlatform() == 3) {//是否为京东
                        Uri uriJd = Uri.parse(urlAlter);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uriJd);
                        context.startActivity(intent);
                    }
                    //getPlatform()==4,跳到亚马逊
                    if (purchaseWayBeen.get(position).getPlatform() == 4) {//是否亚马逊
                        Uri uriYmx = Uri.parse(urlAlter);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uriYmx);
                        context.startActivity(intent);
                    }

                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return purchaseWayBeen.size();
    }

    public class ShopPriceViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout reOne;
        private ImageView taobaoImage;
        private TextView taobaoPrice, taobaoCankao;

        public ShopPriceViewHolder(View itemView) {
            super(itemView);
            reOne = (RelativeLayout) itemView.findViewById(R.id.reOne_item_shopdetails_price);//放置 商城物价的容器
            taobaoImage = (ImageView) itemView.findViewById(R.id.taobaoImage_item_shopdetails_price);//商城图标
            taobaoPrice = (TextView) itemView.findViewById(R.id.taobaoPrice_item_shopdetails_price);//商城价格
            taobaoCankao = (TextView) itemView.findViewById(R.id.taobaoCankao_item_shopdetails_price);//商城名字
        }
    }

}
